﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AwesomeGame.Weapons
{
    public class Explosion
    {
        public delegate void ExplosionEvent(Explosion sender);
        public event ExplosionEvent Remove;

        public Vector2 Position;
        private Texture2D _sprite;
        private Vector2 _spriteMidpoint;
        private TimeSpan _timeCreated = new TimeSpan();
        public long OwnerId { get; set; }

        public Explosion(Vector2 position, long ownerId)
        {
            OwnerId = ownerId;
            Position = position;
            _sprite = Game1.ContentManager.Load<Texture2D>(@"Weapons\Explosion");
            _spriteMidpoint = new Vector2(_sprite.Width / 2, _sprite.Height / 2);
        }

        public void Update(GameTime gameTime)
        {
            if (_timeCreated == new TimeSpan())
                _timeCreated = gameTime.TotalGameTime;
            if (gameTime.TotalGameTime - _timeCreated >= new TimeSpan(0, 0, 5))
                if (Remove != null)
                    Remove(this);
        }

        public void Draw(GameTime gameTime)
        {
            Vector2 screenPosition = Camera.GetScreenPosition(Position);
            Game1.SpriteBatch.Draw(_sprite, screenPosition, null, Color.White, 0f, _spriteMidpoint, 1f, SpriteEffects.None, 0.8f);
        }

        public Rectangle GetCollisionRectangle()
        {
            return new Rectangle((int)(Position.X - _spriteMidpoint.X), (int)(Position.Y - _spriteMidpoint.Y), _sprite.Width, _sprite.Height);
        }

    }
}
